﻿using System;

using BLL;
using Model;
using Utility;

namespace EvaluationSystem
{
    public partial class AddEnterpriseUser : System.Web.UI.Page
    {

        //private int userId = 0;
        //private  string userName = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((SiteSystemMaster)Master).InitMenu();
            ((SiteSystemMaster)Master).li1.Attributes.Add("class", "hover ccc");
            ((SiteSystemMaster)Master).b_hmenu1.Style.Clear();
            ((SiteSystemMaster)Master).b_hmenu2.Style.Add("display", "none");
            ((SiteSystemMaster)Master).b_hmenu3.Style.Add("display", "none");
            ((SiteSystemMaster)Master).s_menu1_2.Style.Add("COLOR", "#147ab8");
            ((SiteSystemMaster)Master).s_menu1_2.Style.Add("TEXT-DECORATION", "none");
            btnSave.Attributes.Add("onclick", "return Check();");
            /*判断是否是管理员身份*/
            if (Session["username"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script>alert('请先登录系统!');top.location.href='../System/Login.aspx';</script>");
                return;
            }
            else
            {
                if (!IsPostBack)
                {
                    if (Request.QueryString["users_id"] != null)
                    {
                        int userId = int.Parse(Request.QueryString["users_id"]);
                        string userName = Request.QueryString["users_name"];
                        lblTitle.Text = "保存企业用户信息";
                        UserName.Text = userName;

                        UserName.ReadOnly = true;
                        CompanyName.Text = Request.QueryString["users_incname"];
                        txtbPhone.Text = Request.QueryString["users_phone"];
                        txtbContact.Text = Request.QueryString["users_contact"];
                    }
                    else
                    {
                        lblTitle.Text = "新建企业用户信息";
                    }

                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int userId = 0;

            if (Request.QueryString["users_id"] != null)
            {
                userId = int.Parse(Request.QueryString["users_id"]);
            }

            // *验证用户输入的信息*/
            if (string.IsNullOrEmpty(UserName.Text))
            {
                MessageBox.ResponseScript(this, "您好，请填写用户名!");
            }
            if (string.IsNullOrEmpty(Password.Text))
            {
                MessageBox.ResponseScript(this, "您好，请填写密码!");
            }
            if (string.IsNullOrEmpty(CompanyName.Text))
            {
                MessageBox.ResponseScript(this, "您好，请填写公司名称!");
            }

            EnterpriseUserBLL enterpriseUserBll = new EnterpriseUserBLL();
            EnterpriseUser enterpriseUser = new EnterpriseUser();
            if (0 == userId)
            {
                if (enterpriseUserBll.Exists(UserName.Text.Trim().ToLower()))
                {
                    MessageBox.ShowAndRedirect(this, "用户添加失败:此用户名已存在", "AddEnterpriseUser.aspx");
                    return;
                }
                enterpriseUser.Username = UserName.Text.Trim().ToLower();
                enterpriseUser.Users_incname = CompanyName.Text.Trim();
                enterpriseUser.Password = Password.Text.Trim().ToLower();
                enterpriseUser.Users_phone = txtbPhone.Text.Trim();
                enterpriseUser.Users_contact = txtbContact.Text.Trim();

                if (enterpriseUserBll.Add(enterpriseUser) != -1)
                {
                    MessageBox.ShowAndRedirect(this, "企业用户添加成功", "EnterpriseUserList.aspx");
                }
                else
                {
                    MessageBox.ShowAndRedirect(this, "企业用户添加失败", "EnterpriseUserList.aspx");
                }
            }
            else
            {
                enterpriseUser.Id = userId;
                enterpriseUser.Username = UserName.Text.Trim();
                enterpriseUser.Users_incname = CompanyName.Text.Trim();
                enterpriseUser.Password = Password.Text.Trim();
                enterpriseUser.Users_phone = txtbPhone.Text.Trim();
                enterpriseUser.Users_contact = txtbContact.Text.Trim();
                if (enterpriseUserBll.Update(enterpriseUser) == 1)
                {
                    MessageBox.ShowAndRedirect(this, "企业用户修改成功", "EnterpriseUserList.aspx");
                }
                else
                {
                    MessageBox.ShowAndRedirect(this, "企业用户修改失败", "EnterpriseUserList.aspx");
                }
            }

        }

        protected void UserName_TextChanged(object sender, EventArgs e)
        {
            EnterpriseUserBLL enterpriseUserBll = new EnterpriseUserBLL();
            if (enterpriseUserBll.Exists(UserName.Text.Trim().ToLower()))
            {
                lblMessage.Text = "该用户名已经注册";
            }
            else
            {
                lblMessage.Text = "";
            }
        }


        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("EnterpriseUserList.aspx");
        }
    }
}